import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'

//element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//路由
import VueRouter from 'vue-router'
import routes from './routes'
//axios
import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios
import '@/common/permissions'

//等会在删除你
// import Mock from './mock'
// Mock.bootstrap();

import store from './vuex/store'
import Vuex from 'vuex'

import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

const router = new VueRouter({
  routes
})

//前端的路由拦截器
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login' || to.path == '/register') {
    //sessionStorage.removeItem('user');
    //todo
    next()//放行
  }else{
    //判断你有没有登录
    let token = sessionStorage.getItem("token")
    if(token){
      next()
    }else {
      next({ path: '/login' })
    }
    //let user = JSON.parse(sessionStorage.getItem('user')); //将json转为对象
    // if (!user && to.path != '/login') {
    //   next({ path: '/login' })
    // } else {
    //   next()
    // }
  }


})

//前置拦截器
axios.interceptors.request.use(config=>{
  initIndexRouters()
  //携带token
  let token =  sessionStorage.getItem("token");
  if(token){
    config.headers['ToKen']=token;
  }
  return config;
},error => {
  Promise.reject(error);
})

//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
  if(result.data.success && result.data.msg =='managerLogin'){
    sessionStorage.setItem("token",result.data.data.token)
    sessionStorage.setItem("userinfo",JSON.stringify(result.data.data.logininfo))
    sessionStorage.setItem("employee",JSON.stringify(result.data.data.employee))
    sessionStorage.setItem("user",JSON.stringify(result.data.data.user))
    sessionStorage.setItem("menus",JSON.stringify(result.data.data.menus))
    sessionStorage.setItem("permissions",JSON.stringify(result.data.data.permissions))
    initIndexRouters()
  }
  if(!result.data.success && result.data.msg == 'noLogin' ){
    sessionStorage.removeItem("token")
    sessionStorage.removeItem("userinfo")
    sessionStorage.removeItem("employee")
    sessionStorage.removeItem("user")
    sessionStorage.removeItem("permissions")
    sessionStorage.removeItem("menus")
    routes.push({ path: '/login' })
  }
  if(!result.data.success && result.data.msg == 'noPermission' ){
    alert("你没有权限，滚蛋")
  }
  // let data = result.data;
  // if(!data.success && data.message==="noUser")
  //   location.href = "/login.html";
  return result;//一定要返回
},error => {
  Promise.reject(error);
})


//后置拦截器
//router.afterEach(transition => {
//NProgress.done();
//});

initIndexRouters();
function initIndexRouters(){
  if(!sessionStorage.menus){
    return;
  }
  //防止重复配置路由
  if(router.options.routes.length>6){
    return;
  }
  let menus = sessionStorage.getItem('menus');
  menus = JSON.parse(menus);
  console.log(menus)
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      name: menu.name,
      iconCls: menu.icon,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  // console.log(this.$router)
  router.addRoutes(tempRouters);
}

//绑定挂载节点
new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

